import { createRouter, createWebHistory } from "vue-router";
import Login from "../views/Login.vue";
import Dashboard from "../views/Dashboard.vue";
// 二级路由
import Home from "../views/DashboardChildren/Home.vue";
import OrderManagement from "../views/DashboardChildren/OrderManagement.vue";
import ShopManagement from "../views/DashboardChildren/ShopManagement.vue";
// 商品管理子路由
const ProductList = () =>
  import(
    "../views/DashboardChildren/ProductManagementChildren/ProductList.vue"
  );
const ProductAdd = () =>
  import("../views/DashboardChildren/ProductManagementChildren/ProductAdd.vue");
const ProductCategory = () =>
  import(
    "../views/DashboardChildren/ProductManagementChildren/ProductCategory.vue"
  );

// 账号管理子路由
const AccountList = () =>
  import(
    "../views/DashboardChildren/AccountManagementChildren/AccountList.vue"
  );
const AccountAdd = () =>
  import("../views/DashboardChildren/AccountManagementChildren/AccountAdd.vue");
const ChangePassword = () =>
  import(
    "../views/DashboardChildren/AccountManagementChildren/ChangePassword.vue"
  );
const PersonalCenter = () =>
  import(
    "../views/DashboardChildren/AccountManagementChildren/PersonalCenter.vue"
  );

// 销售统计子路由
const ProductStats = () =>
  import("../views/DashboardChildren/SalesStatisticsChildren/ProductStats.vue");
const OrderStats = () =>
  import("../views/DashboardChildren/SalesStatisticsChildren/OrderStats.vue");

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "Login",
      component: Login,
    },
    {
      path: "/dashboard",
      name: "Dashboard",
      component: Dashboard,
      children: [
        {
          path: "home",
          name: "Home",
          component: Home,
          meta: {
            id: "1",
            name: "后台首页",
            icon: "Menu",
            path: "/dashboard/home",
            title: "Home",
          },
        },
        {
          path: "order",
          name: "OrderManagement",
          component: OrderManagement,
          meta: {
            id: "2",
            name: "订单管理",
            icon: "List",
            path: "/dashboard/order",
            title: "OrderManagement",
          },
        },
        {
          path: "product",
          name: "ProductManagement",
          meta: {
            id: "3",
            name: "商品管理",
            icon: "ShoppingCartFull",
            path: "/dashboard/product",
            title: "ProductManagement",
          },
          children: [
            {
              path: "list",
              name: "ProductList",
              component: ProductList,
              meta: {
                id: "1",
                name: "商品列表",
                icon: "List",
                path: "/dashboard/product/list",
                title: "ProductList",
              },
            },
            {
              path: "add",
              name: "ProductAdd",
              component: ProductAdd,
              meta: {
                id: "2",
                name: "商品添加",
                icon: "Plus",
                path: "/dashboard/product/add",
                title: "ProductAdd",
              },
            },
            {
              path: "category",
              name: "ProductCategory",
              component: ProductCategory,
              meta: {
                id: "3",
                name: "商品分类",
                icon: "Files",
                path: "/dashboard/product/category",
                title: "ProductCategory",
              },
            },
          ],
        },
        {
          path: "shop",
          name: "ShopManagement",
          component: ShopManagement,
          meta: {
            id: "4",
            name: "店铺管理",
            icon: "Shop",
            path: "/dashboard/shop",
            title: "ShopManagement",
          },
        },
        {
          path: "account",
          name: "AccountManagement",
          meta: {
            id: "5",
            name: "账号管理",
            icon: "Notebook",
            path: "/dashboard/account",
            title: "AccountManagement",
          },
          children: [
            {
              path: "list",
              name: "AccountList",
              component: AccountList,
              meta: {
                id: "1",
                name: "账号列表",
                icon: "Document",
                path: "/dashboard/account/list",
                title: "AccountList",
              },
            },
            {
              path: "add",
              name: "AccountAdd",
              component: AccountAdd,
              meta: {
                id: "2",
                name: "添加账号",
                icon: "Plus",
                path: "/dashboard/account/add",
                title: "AccountAdd",
              },
            },
            {
              path: "password",
              name: "ChangePassword",
              component: ChangePassword,
              meta: {
                id: "3",
                name: "修改密码",
                icon: "Edit",
                path: "/dashboard/account/password",
                title: "ChangePassword",
              },
            },
            {
              path: "center",
              name: "PersonalCenter",
              component: PersonalCenter,
              meta: {
                id: "4",
                name: "个人中心",
                icon: "User",
                path: "/dashboard/account/center",
                title: "PersonalCenter",
              },
            },
          ],
        },
        {
          path: "sales",
          name: "SalesStatistics",
          meta: {
            id: "6",
            name: "销售统计",
            icon: "Finished",
            path: "/dashboard/sales",
            title: "SalesStatistics",
          },
          children: [
            {
              path: "product",
              name: "ProductStats",
              component: ProductStats,
              meta: {
                id: "1",
                name: "商品统计",
                icon: "OfficeBuilding",
                path: "/dashboard/sales/product",
                title: "ProductStats",
              },
            },
            {
              path: "order",
              name: "OrderStats",
              component: OrderStats,
              meta: {
                id: "2",
                name: "订单统计",
                icon: "Printer",
                path: "/dashboard/sales/order",
                title: "OrderStats",
              },
            },
          ],
        },
      ],
    },
  ],
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem("t_k"); // 假设token存储在localStorage中

  // 如果用户没有登录并试图从登录页跳转到其他页面，强制回到登录页
  if (to.name !== "Login" && !isLoggedIn) {
    next({ name: "Login" }); // 强制跳转到登录页
  }
  // 如果用户已经登录，且试图访问登录页，跳转到其他页面
  else if (to.name === "Login" && isLoggedIn) {
    localStorage.removeItem("t_k");
    next({ name: "Login" }); // 强制跳转到登录页
  }
  // 否则，允许路由跳转
  else {
    next();
  }
});

export default router;
